package com.yuexunit.operationman.nbsy.module.plant.ui;

import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;

import com.yuexunit.operationman.nbsy.R;
import com.yuexunit.operationman.nbsy.base.SubmitBean;
import com.yuexunit.operationman.nbsy.config.AppConfig;
import com.yuexunit.operationman.nbsy.module.plant.adapter.PlantAdapter;
import com.yuexunit.operationman.nbsy.module.plant.base.ICallBack;
import com.yuexunit.operationman.nbsy.module.plant.bean.ImplantMaterialItemListEntity;
import com.yuexunit.operationman.nbsy.module.plant.bean.MaterialBean;
import com.yuexunit.operationman.nbsy.module.plant.model.impl.MaterialAddModel;
import com.yuexunit.operationman.nbsy.module.plant.model.impl.MaterialUpdateModel;
import com.yuexunit.operationman.nbsy.module.plant.presenter.impl.MaterialAddPresenter;
import com.yuexunit.operationman.nbsy.module.plant.presenter.impl.MaterialUpdatePresenter;
import com.yuexunit.operationman.nbsy.module.plant.ui.view.IMaterialAddView;
import com.yuexunit.operationman.nbsy.module.plant.ui.view.IMaterialUpdateView;
import com.yuexunit.operationman.nbsy.widget.FixedSwipeListView;
import com.yuexunit.operationman.nbsy.widget.LoadingDialog;

import org.kymjs.kjframe.KJActivity;
import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by chenss on 2017/7/11 0011.
 */

public class Act_Plant_Register extends KJActivity implements ICallBack, IMaterialAddView, IMaterialUpdateView {
    @Bind(R.id.usr_name)
    TextView usrName;
    @Bind(R.id.usr_num)
    TextView usrNum;
    @Bind(R.id.usr_ops)
    TextView usrOps;
    @Bind(R.id.listview)
    FixedSwipeListView listview;
    @Bind(R.id.all_price)
    TextView allPrice;

    LoadingDialog loadingDialog;
    PlantAdapter adapter;
    List<ImplantMaterialItemListEntity> materialItemList = new ArrayList<>();
    MaterialAddPresenter addPresenter;
    MaterialUpdatePresenter updatePresenter;
    int type = 0;//0 add 1 update

    @Override
    public void setRootView() {
        setContentView(R.layout.act_plant_register);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        super.initData();
        setTitle();
        loadingDialog = new LoadingDialog(this);
        addPresenter = new MaterialAddPresenter(this, new MaterialAddModel());
        updatePresenter = new MaterialUpdatePresenter(this, new MaterialUpdateModel());
        if(AppConfig.MATERIALBEAN.getObject().getImplantMaterial().getMaterialId() != null){
            if(AppConfig.MATERIALBEAN.getObject().getImplantMaterialItemList() != null){
                materialItemList = AppConfig.MATERIALBEAN.getObject().getImplantMaterialItemList();
            }
            type = 1;
        }else{
            type = 0;
        }
        if(AppConfig.MATERIALBEAN.getObject().getImplantMaterial().getStatus() != null &&
                AppConfig.MATERIALBEAN.getObject().getImplantMaterial().getStatus().equals("1")){
            adapter = new PlantAdapter(aty, materialItemList, this, true);
        }else{
            adapter = new PlantAdapter(aty, materialItemList, this, false);
        }
        listview.setAdapter(adapter);

        getPrice();
    }

    private void setTitle()
    {
        if(AppConfig.MATERIALBEAN.getObject().getImplantMaterial().getPatientSex().equals("1")) {
            usrName.setText(AppConfig.MATERIALBEAN.getObject().getImplantMaterial().getPatientName() + "    男");
        }else if(AppConfig.MATERIALBEAN.getObject().getImplantMaterial().getPatientSex().equals("2"))
        {
            usrName.setText(AppConfig.MATERIALBEAN.getObject().getImplantMaterial().getPatientName() + "    女");
        }
        else {
            usrName.setText(AppConfig.MATERIALBEAN.getObject().getImplantMaterial().getPatientName() + "    不详");
        }
        usrNum.setText("住院号" + AppConfig.MATERIALBEAN.getObject().getImplantMaterial().getInHospitalNo());
        usrOps.setText(AppConfig.MATERIALBEAN.getObject().getImplantMaterial().getOperationName());
    }

    private void getPrice(){
        float price = 0;
        for(ImplantMaterialItemListEntity materialItem : materialItemList){
            price = price + Float.valueOf(materialItem.getPrice()) * Integer.valueOf(materialItem.getAmount());
        }
        allPrice.setText(price + "");
    }

    @OnClick({R.id.back, R.id.add, R.id.sure})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.add:
                Intent intent = new Intent(this, Act_Scan_code.class);
                aty.startActivityForResult(intent, 1);
                break;
            case R.id.sure:
                if(type == 0){
                    insert();
                }else{
                    update();
                }

                break;
        }
    }

    private void insert(){
        MaterialBean materialBean = AppConfig.MATERIALBEAN;
        materialBean.getObject().setImplantMaterialItemList(materialItemList);
        Map<String, String> params = new HashMap<>();
        params.put("saveData", JSON.toJSONString(materialBean));
        params.put("operationNo", AppConfig.MATERIALBEAN.getObject().getImplantMaterial().getOperationNo());
        params.put("interType", AppConfig.INTERTYPE);
        addPresenter.insertMaterial(params);
    }

    private void update(){
        MaterialBean materialBean = AppConfig.MATERIALBEAN;
        materialBean.getObject().setImplantMaterialItemList(materialItemList);
        Map<String, String> params = new HashMap<>();
        params.put("saveData", JSON.toJSONString(materialBean));
        params.put("accountId", PreferenceHelper.readInt(aty, AppConfig.USER_INFO, AppConfig.USER_ACCOUNT) + "");
        params.put("fullName", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.USER_FULLNAME) + "");
        params.put("interType", AppConfig.INTERTYPE);
        updatePresenter.UpdateMaterial(params);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == 10000){
            ImplantMaterialItemListEntity materialItem = (ImplantMaterialItemListEntity) data.getExtras().getSerializable("materialItem");
            for(ImplantMaterialItemListEntity material : materialItemList){
                if(material.getNumber().equals(materialItem.getNumber())){
                    if(material.getBatchNumber().equals(materialItem.getBatchNumber())){
                        int amount = Integer.valueOf(material.getAmount()) + 1;
                        material.setAmount(amount + "");
                        getPrice();
                        adapter.setMaterialItemList(materialItemList);
                        return;
                    }
                }
            }
            materialItemList.add(materialItem);
            getPrice();
            adapter.setMaterialItemList(materialItemList);
        }
    }

    @Override
    public void callBack() {
        getPrice();
    }

    @Override
    public void insertMaterial(SubmitBean submitBean) {
        if(submitBean.getCode() == 200){
            AppConfig.MATERIALBEAN.getObject().setImplantMaterialItemList(materialItemList);
            ViewInject.toast("新建成功");
            finish();
        }
    }

    @Override
    public void loadingDialogStatus(int status) {
        switch (status) {
            case AppConfig.SHOW_DIALOG:
                loadingDialog.setCanceledOnTouchOutside(false);
                loadingDialog.show();
                break;
            case AppConfig.DISMISS_DIALOG:
                loadingDialog.dismiss();
                break;

        }
    }

    @Override
    public void updateMaterial(SubmitBean submitBean) {
        if(submitBean.getCode() == 200){
            AppConfig.MATERIALBEAN.getObject().setImplantMaterialItemList(materialItemList);
            ViewInject.toast("更新成功");
            finish();
        }
    }
}
